package com.bjpower.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpower.common.enums.SysCode;
import com.bjpower.convent.ProductVoConvent;
import com.bjpower.domain.PayProduct;
import com.bjpower.models.Result;
import com.bjpower.models.request.ProductAddRequest;
import com.bjpower.models.request.UserPageInfoRequest;
import com.bjpower.models.vo.ProductPageVo;
import com.bjpower.services.ProductListService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
public class productListController {

    @Resource
    private ProductListService productListService;

    @Resource
    private ProductVoConvent productVoConvent;

    @GetMapping("/product/pageList")
    public Result ProductPageList(UserPageInfoRequest pageInfoRequest){
        IPage<PayProduct> payProductList = productListService.getPayProductList(pageInfoRequest);
        List<PayProduct> listRecords = payProductList.getRecords();
        long pages = payProductList.getPages();
        long current = payProductList.getCurrent();
        List<ProductPageVo> list = productVoConvent.toProductPageVoList(listRecords);
        HashMap<String, Object> data = new HashMap<>();
        data.put("list", list);
        data.put("pages", pages);
        data.put("current", current);
        return Result.ok(data);
    }

    @PostMapping("/product/add")
    public Result addPayProduct(@RequestBody @Validated ProductAddRequest productAddRequest){
        productListService.addPayProductService(productAddRequest);
        return Result.ok();
    }

    /**
     *  根据产品名称模糊查询产品列表
     */
    @GetMapping("/merch/productList")
    public Result getMerchProductList(String productName){
        if (StrUtil.isNotBlank(productName)){
            List<PayProduct> merchProductList = productListService.selectMerchProductList(productName);
            List<ProductPageVo> merchProductVoList = productVoConvent.toProductPageVoList(merchProductList);
            return Result.ok(merchProductVoList);
        }
        return Result.error(SysCode.REQUEST_PARAM_ERROR);
    }
}
